//
//  FourCell.m
//  ZhiBoYuan
//
//  Created by apple on 2018/4/18.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "FourCell.h"
#import "FourCollectionViewCell.h"
#import "AdetailViewController.h"
#import "Header.h"
#import "BannerModel.h"
static NSString *ident = @"FourCollectionViewCell";

@interface FourCell ()
<
UICollectionViewDelegate,
UICollectionViewDataSource
>
@property (strong, nonatomic)  UICollectionView *collectionView;
//@property (nonatomic,strong)

@property(nonatomic,strong)UIPageControl *pageControl;
@end

@implementation FourCell
#define JianGe 10
#define GeShu 3

- (void)setAdArray:(NSMutableArray *)adArray{
    _adArray = adArray;
//     _pageControl.numberOfPages = self.adArray.count;
    [self.collectionView reloadData];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
     _hight.constant = (ScreenWidth - JianGe * (GeShu+1) ) / GeShu + 10;
    
//    self.contentView.backgroundColor = [UIColor blackColor];
//
//    self.backgroundColor = [UIColor blackColor];
    
//    _hight.constant = FourClassHight;
    
    if (self.adArray == nil) {
        self.adArray = [NSMutableArray array];
    }
    
    
    UIView *view =[UIView new];
    
    view.backgroundColor = hui1Color;
    [self addSubview:view];
    
    view.sd_layout
    .topSpaceToView(self, 0)
    .leftSpaceToView(self, 5)
    .rightSpaceToView(self, 5)
    .heightIs(1);
    
    
    
    
    
    [self addSubview:self.collectionView];
    
    self.collectionView.sd_layout
    .topSpaceToView(view, 0)
    .leftSpaceToView(self, 0)
    .rightSpaceToView(self, 0)
    .bottomSpaceToView(self, 0);
    
//    [self addSubview:self.pageControl];
    
//    self.pageControl.sd_layout
//    .heightIs(20)
//    .leftSpaceToView(self, 0)
//    .rightSpaceToView(self, 0)
//    .bottomSpaceToView(self, 0);
    


}

-(UIPageControl *)pageControl{
    if (!_pageControl) {
        
        _pageControl = [[UIPageControl alloc] init];
        _pageControl.frame = CGRectMake(0 , 0, 80, 20);//指定位置大小
        [_pageControl setCenter:CGPointMake(self.frame.size.width/2-40, self.center.x+20)];
        
        [_pageControl setCenter:self.center];
//        _pageControl.numberOfPages = 3;//指定页面个数
        _pageControl.currentPage = 0;//指定pagecontroll的值，默认选中的小白点（第一个）
        //添加委托方法，当点击小白点就执行此方法
        _pageControl.pageIndicatorTintColor = hui5Color;
        // 设置非选中页的圆点颜色
        _pageControl.currentPageIndicatorTintColor =ZhuTiColor ; // 设置选中页的圆点颜色 [self.view addSubview:_pageControl];
        
    }
    return _pageControl;
}

-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.itemSize = CGSizeMake(130, 130);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = 2;
        UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 244, 0,130) collectionViewLayout:layout];
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.scrollsToTop = NO;
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        [collectionView registerNib:[UINib nibWithNibName:ident bundle:nil] forCellWithReuseIdentifier:ident];

        _collectionView = collectionView;
        

    }
    return _collectionView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.adArray.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    FourCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:ident forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor whiteColor];
    cell.banner = self.adArray[indexPath.row];
    return cell;
}





//定义每个UICollectionView 的大小

- ( CGSize )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:( NSIndexPath *)indexPath
{
    
    return CGSizeMake(( ScreenWidth - JianGe * ( GeShu+1 )) / 2 - 30,
                      ( ScreenWidth - JianGe * ( GeShu+1 )) / GeShu + 10 );
    
}



//定义每个UICollectionView 的边距

- ( UIEdgeInsets )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:( NSInteger )section {

    return UIEdgeInsetsMake ( JianGe , JianGe , JianGe , JianGe );

}

//设置水平间距 (同一行的cell的左右间距）

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {

    return JianGe;

}

//垂直间距 (同一列cell上下间距)

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {

    return JianGe;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    BannerModel *banner = self.adArray[indexPath.row];
    if (self.clickBanner) {
        self.clickBanner(banner);
    }

}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

@end
